package com.jackiechan.day17;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test2 {
    public static void main(String[] args) throws Exception {
//        method();
//        method1();
//        method2();
//        method3();
        haha();
    }

    private static void method3() throws Exception {
        Class c = PrivateClass.class;
        Constructor cons = c.getDeclaredConstructor();
        cons.setAccessible(true);
        // 构造方法
        Object obj = cons.newInstance();
        for (Method method :c.getDeclaredMethods()) {
            System.out.println(method.getName());
            method.setAccessible(true);
            method.invoke(obj);
        }
        for (Field field :c.getDeclaredFields()) {
            field.setAccessible(true);
            System.out.println(field.get(obj));
            field.set(obj,500);
            System.out.println(field.get(obj));
        }

    }

    // 调用成员
    private static void method2() throws Exception {
        Class c = Student.class;
        Object o = c.newInstance();
        Field f = c.getField("by");
        System.out.println(f.getName());
        System.out.println(f.getType().getName());
        System.out.println(f.get(o));
        f.set(o,(byte)56);
        System.out.println(f.get(o));
    }
    // 调用方法
    private static void method1() throws Exception {
        Class c = Student.class;
        Object o = c.newInstance();
        Method m = c.getDeclaredMethod("play");
        m.setAccessible(true);
        m.invoke(o);
    }
    // 创建对象
    private static void method() throws Exception {
        Class c = Student.class;
        Object o = c.newInstance();
        System.out.println(o);
//        Method m = c.getMethod("show");
//        m.setAccessible(true);

    }

    private static void haha() throws Exception {
        Class c = Student.class;
        Constructor[] constructors = c.getDeclaredConstructors();
//        Constructor constructor1 = c.getConstructor(double.class);
        Method[] methods = c.getDeclaredMethods();
//        Method method1 = c.getMethod("playgame");
        Field[] fields = c.getDeclaredFields();
        System.out.println("**********");
        for (Constructor constructor :constructors) {
            System.out.println(constructor);
//            constructor.setAccessible(true);
//            Object object = constructor.newInstance();
//            Object o = c.newInstance();
//            Class[] parameterTypes = constructor.getParameterTypes();
        }
        System.out.println("**********");
        for (Method method :methods) {
            System.out.println(method.getName());
//            method.setAccessible(true);
//            method.invoke(new Student());
        }
        System.out.println("**********");
        for (Field field :fields) {
            System.out.println(field.getType()+" "+field.getName());
//            field.set(new Student(),23);
//            field.get(new Student());
        }

    }
}

